#pragma once

#include "IXIconMgr.h"
#include "XBaseObjImpl.h"
#include <map>

struct stXIconIndex
{
    BOOL    bBig;
    XString strPath;
};

struct stXIconInfo
{
    int     nRef;
    XICON   xIcon;
};

__inline bool operator < (const stXIconIndex& index1, const stXIconIndex& index2)
{
    if(index1.bBig == index2.bBig)
    {
        return index1.strPath < index2.strPath;
    }
    else
    {
        return index1.bBig < index2.bBig;
    }
}


class CXIconMgrImpl : public CXBaseObjImpl<IXIconMgr>
{
    typedef CXBaseObjImpl<IXIconMgr> superclass;
    DEF_SKIN_CLASS_SINGLE_INTF(CXIconMgrImpl,
        superclass,
        gs_strGuidXSkin,
        IXIconMgr,
        eIntf_Singleton);
public:
    CXIconMgrImpl();
    virtual ~CXIconMgrImpl();

public:
    // IXIconMgr
    virtual HRESULT STDMETHODCALLTYPE   LoadIcon(IN LPCTSTR szXNC, IN BOOL bBigIcon, OUT XICON* pIcon);
    virtual HRESULT STDMETHODCALLTYPE   ReleaseIcon(IN XICON Icon);
    virtual HRESULT STDMETHODCALLTYPE   SaveIcon(IN XICON Icon, IN LPCTSTR szFilePath);

public:
    // IBaseObj
    virtual void    STDMETHODCALLTYPE   OnFinalRelease();

protected:
    void    ReleaseData();
    XICON   LoadIconHelper(IN LPCTSTR szXNC, BOOL bBigIcon);

protected:
    typedef std::map<stXIconIndex, stXIconInfo>   IconContainer;
    IconContainer  m_IconContainer;
};

